package com.woniuxy.web.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.woniuxy.entity.Dish;
import com.woniuxy.entity.DishCategory;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Specs;
import com.woniuxy.service.SpecsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin(value="http://192.168.6.186:8080",allowCredentials = "true")
@Slf4j
@RestController
@JsonIgnoreProperties("handler")
@RequestMapping("admin/specs")
public class SpecsController_b {
    @Autowired
    private SpecsService specsService;

    @PostMapping("add")
    public ResponseEntity add(@RequestBody Specs specs){
        if(specsService.getByName(specs.getName()) != null){
            return ResponseEntity.FIAL;
        }else {
            specsService.add(specs);
            return ResponseEntity.SUCCESS;
        }
    }

    @GetMapping("pageList")
    public ResponseEntity<List<Specs>> list(Integer current, String query, Integer pageSize, Specs specs){
        if(current ==null || current == 0){
            current=1;
        }
        if(pageSize == null || pageSize == 0){
            pageSize=2;
        }
        //条件筛选
        QueryWrapper<Specs> queryWrapper =new QueryWrapper<>();
        if(query !=null && query.trim().length() !=0){
            queryWrapper.like("name",query);
        }
        if (specs.getValue() !=null && specs.getValue().trim().length() !=0){
            queryWrapper.eq("value",specs.getValue());
        }
        queryWrapper.eq("status","y");
        Page<Specs> page=new Page<>(current,2);
        page=specsService.page(page,queryWrapper);

        return new ResponseEntity(page);
    }

    @GetMapping("get/{id}")
    public ResponseEntity<Specs> get(@PathVariable int id){

        return new ResponseEntity<>(specsService.getById(id));
    }

    /**
     * 删除对应id的规格
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public ResponseEntity del(@PathVariable int id) {
        specsService.removeById(id);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping ("update")
    public ResponseEntity<?> update(@RequestBody Specs specs){
        specs.setStatus("y");
        specsService.update(specs);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("list")
    public ResponseEntity<List<Specs>> list(){

        return new ResponseEntity<List<Specs>>( specsService.getAll());

    }

    @PostMapping("get")
    public ResponseEntity getById(@RequestBody Specs specs){

        return new ResponseEntity(specsService.getById(specs.getId()));
    }

    @PostMapping("getByspecArrar")
    public ResponseEntity<List<Specs>> getByspecArrar(@RequestBody Dish dish){
        String specs = dish.getSpec_array();
        String[] specArray = null;
        if(specs.contains(",")){
            specArray = specs.split(",");
        }

       List<Specs> specList = new ArrayList<>();
        for(String specId : specArray){
            if(!specId.equals("")) {
                specList.add(specsService.getById(specId));
            }
        }
        return new ResponseEntity<List<Specs>>(specList);
    }
}
